package com.httpDemo.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 切面类
 * 
 * @author 86173
 *
 */
@Component
@Aspect
public class MyAspect {

	@Pointcut(value = "execution(* com.httpDemo.doMain.Man.*(..))")
	public void pc() {

	}

//	@Before(value = "pc()")
	public void before(String food) {
		System.out.println("吃" + food + "前，吃点水果！");
	}

//	@After(value = "pc()")
	public void after(String food, String result) {
		System.out.println("吃完" + food + result + "，搞一根！");
	}

	/**
	 * 环绕通知
	 * 
	 * @param joinPoint
	 */
	@Around(value = "pc()")
	public void around(ProceedingJoinPoint joinPoint) {
		try {
			Object[] args = joinPoint.getArgs();// 获取请求参数
			String food = args[0].toString();
			before(food);// 目标方法执行前的方法
			Object proceed = joinPoint.proceed();// 执行目标方法
			String result = proceed.toString();// 获取响应参数
			after(food, result);// 目标方法执行后的方法
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	/**
	 * 异常通知
	 * 
	 * @param tw
	 */
	@AfterThrowing(pointcut = "pc()", throwing = "tw")
	public void error(Throwable tw) {
		System.out.println("目标出现异常！");
	}
}
